<template>

    <a-form ref="formRef" name="custom-validation" :model="formState" v-bind="layout" @finish="handleFinish">
        <a-row>
            <a-col :span="4">
                <a-form-item has-feedback label="客户" name="cust_id">
                    <a-select allow-clear v-model:value="formState.cust_id">
                        <a-select-option :value="item.key" v-for="item in queryCustList" :key="item.key">{{
                            item.name
                        }}
                        </a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>

            <a-col :span="4">
                <a-form-item has-feedback label="跟进人员" name="emp_id">
                    <a-select allow-clear v-model:value="formState.emp_id">
                        <a-select-option :value="item.key" v-for="item in queryEmpList" :key="item.key">{{
                            item.name
                        }}
                        </a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>


            <a-col :span="4">
                <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
                    <a-button type="primary" html-type="submit">查询</a-button>
                    <!-- <a-button type="primary" style="margin-left: 10px" @click="handleAdd">新增</a-button> -->
                </a-form-item>
            </a-col>
        </a-row>

        <a-row>
            <a-col :span="24">
                <a-flex justify="flex-end">
                    <a-button type="primary" style="margin-left: 10px" @click="handleAdd">新增</a-button>
                </a-flex>
            </a-col>
        </a-row>
    </a-form>


    <a-row>
        <a-col :span="24">
            <a-table size="small" style="width: 100%;" :columns="columns" :data-source="data">
                <template #bodyCell="{ column, text, record }">
                    <template v-if="column.dataIndex === 'name'">
                        <a>{{ text }}</a>
                    </template>
                    <template v-if="column.dataIndex === 'action'">
                        <a @click="handleEdit(record)">编辑</a>
                        <a @click="handleDelete(record)">删除</a>
                    </template>
                </template>
            </a-table>
        </a-col>
    </a-row>

    <a-row>
        <a-drawer v-model:open="open" class="custom-class" root-class-name="root-class-name" style="color: red"
            :title="addFormState.task_id ? '编辑' : '新增'" placement="right">
            <a-form :rules="addRulesForm" ref="addFormRef" name="custom-validation" :model="addFormState"
                v-bind="layout" @finish="addFormState.task_id ? handleEditFinish($event) : handleAddFinish($event)">

                <a-form-item has-feedback label="客户" name="cust_id">
                    <a-select v-model:value="addFormState.cust_id">
                        <a-select-option :value="item.key" v-for="item in custList" :key="item.key">{{
                            item.name
                        }}
                        </a-select-option>
                    </a-select>
                </a-form-item>

                <a-form-item has-feedback label="关联消费记录" name="consume_id">
                    <a-select v-model:value="addFormState.consume_id">
                        <a-select-option :value="item.key" v-for="item in consumeList" :key="item.key">{{
                            item.name
                        }}
                        </a-select-option>
                    </a-select>
                </a-form-item>

                <a-form-item has-feedback label="跟进人员" name="emp_id">
                    <a-select v-model:value="addFormState.emp_id">
                        <a-select-option :value="item.key" v-for="item in empList" :key="item.key">{{ item.name
                        }}
                        </a-select-option>
                    </a-select>
                </a-form-item>

                <a-form-item has-feedback label="预计回访日期" name="plan_date">
                    <a-date-picker format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" show-time
                        style="width: 100%" v-model:value="addFormState.plan_date" />
                </a-form-item>


                <a-form-item has-feedback label="回访目的" name="purpose">
                    <a-textarea v-model:value="addFormState.purpose" />
                </a-form-item>

                <a-form-item has-feedback>
                    <a-col :span="24">
                        <a-flex justify="flex-end">
                            <a-button type="primary" style="margin-left: 10px" html-type="submit">保存</a-button>
                        </a-flex>
                    </a-col>
                </a-form-item>
            </a-form>
        </a-drawer>
    </a-row>
</template>
<script setup>
import { revisit_task as service } from '@/api/modulesApi/revisit_task';
import { onMounted, ref, reactive } from 'vue';
import { message } from 'ant-design-vue';

const custList = ref([]);
const getCustList = async () => {
    const res = await service.getCustList();
    custList.value = res.data;
};

const consumeList = ref([]);
const getConsumeList = async () => {
    const res = await service.getConsumeList();
    consumeList.value = res.data;
};

const empList = ref([]);
const getEmpList = async () => {
    const res = await service.getEmpList();
    empList.value = res.data;
};

const queryCustList = ref([]);
const getQueryCustList = async () => {
    const res = await service.getQueryCustList();
    queryCustList.value = res.data;
};

const queryEmpList = ref([]);
const getQueryEmpList = async () => {
    const res = await service.getQueryEmpList();
    queryEmpList.value = res.data;
};

const columns = [
    {
        title: '任务编号',
        dataIndex: 'task_id',
        key: 'task_id',
    },
    {
        title: '客户',
        dataIndex: 'cust_id',
        key: 'cust_id',
        customRender: ({ text, record }) => {
            return record.cust_name
        }
    },
    {
        title: '关联消费记录',
        dataIndex: 'consume_id',
        key: 'consume_id',
    },
    {
        title: '跟进人员',
        dataIndex: 'emp_id',
        key: 'emp_id',
        customRender: ({ text, record }) => {
            return record.emp_name
        }
    },
    {
        title: '预计回访日期',
        dataIndex: 'plan_date',
        key: 'plan_date',
    },
    {
        title: '回访状态',
        dataIndex: 'status',
        key: 'status',
    },
    {
        title: '回访目的',
        dataIndex: 'purpose',
        key: 'purpose',
    },
    {
        title: '操作',
        dataIndex: 'action',
        key: 'action',
    }
];
const data = ref([]);

const formRef = ref();
const formState = reactive({
    cust_id: '',
    emp_id: '',
});

const open = ref(false);

const addFormState = reactive({
    cust_id: '',
    emp_id: '',
    plan_date: '',
    purpose: '',
    consume_id: '',
});
const addFormRef = ref();

const addRulesForm = {
    cust_id: [{ required: true, message: '请选择客户' }],
    consume_id: [{ required: true, message: '请选择关联消费记录' }],
    emp_id: [{ required: true, message: '请选择跟进人员' }],
    plan_date: [{ required: true, message: '请选择预计回访日期' }],
};

const handleAdd = () => {
    addFormState.task_id = '';
    addFormState.cust_id = '';
    addFormState.emp_id = '';
    addFormState.plan_date = '';
    addFormState.purpose = '';
    addFormState.consume_id = '';
    open.value = true;
};

const handleAddFinish = async (values) => {
    const params = {
        cust_id: values.cust_id,
        emp_id: values.emp_id,
        plan_date: values.plan_date,
        purpose: values.purpose,
        consume_id: values.consume_id,
    };
    const res = await service.addRevisitTask(params);
    getRevisitTask();
    open.value = false;
    message.success('新增成功');
};

const handleEditFinish = async (values) => {
    const params = {
        cust_id: values.cust_id,
        emp_id: values.emp_id,
        plan_date: values.plan_date,
        purpose: values.purpose,
        consume_id: values.consume_id,
        task_id: addFormState.task_id,
    };
    const res = await service.updateRevisitTask(params);
    getRevisitTask();
    open.value = false;
    message.success('编辑成功');
};

const handleDelete = async (record) => {
    const res = await service.deleteRevisitTask({ task_id: record.task_id });
    getRevisitTask();
    message.success('删除成功');
};

const handleEdit = (record) => {
    addFormState.task_id = record.task_id;
    addFormState.cust_id = record.cust_id;
    addFormState.emp_id = record.emp_id;
    addFormState.plan_date = record.plan_date;
    addFormState.purpose = record.purpose;
    addFormState.consume_id = record.consume_id;
    open.value = true;
};

const layout = {
    labelCol: { span: 7 },
    wrapperCol: { span: 14 },
};

const handleFinish = (values) => {
    getRevisitTask();
};

const getRevisitTask = async () => {
    const params = {
        cust_id: formState.cust_id,
        emp_id: formState.emp_id,
    };
    const res = await service.getRevisitTask(params);
    data.value = res.data;
};

onMounted(() => {
    getRevisitTask();
    getCustList();
    getConsumeList();
    getEmpList();
    getQueryCustList();
    getQueryEmpList();
});
</script>